import Constants from "./platform/Constants";
import store from "./platform/StorageUtil";
import HttpClient from "./HttpClient";
import ChainInfo from "./EosChainInfo";
let Eos = {};
if(Constants.platform === 'mobile') {
    import('./platform/EosUtil').then(module => Eos = module);
}else{
    import('./platform/EosUtil').then(module => Eos = module);
}

// let pf = 'platform';
// let Eos = require('./'+pf+'/EosUtil');

const test = ()=>{
    Eos.randomPrivateKey((data)=>{});
};

//-----account-----
const accountCreate = (payload, callback) => {
    try {
        let req = {
            newAccount:payload.newAccount,
            ownerPublicKey:payload.ownerPublicKey,
            activePublicKey:payload.activePublicKey,
            code:payload.code
        };
        let url = Constants.SERVERURL+'/account/create';
        HttpClient.request(url,'post',req, false, (data)=>{
            if(callback){callback(data)}
        });
    } catch (error) {
        if (callback) callback({ code: 500, msg: "创建账号失败" });
    }
};

//-----vote-----
const producerRecommendList = (payload,callback,cache=true) => {
    //超级节点推荐
    let req = {
        first:payload.first || 0,
        pageSize:payload.pageSize || 10,
        name:payload.name || '',
    };
    let url = Constants.SERVERURL+'/productor/getListOrderByVote';
    HttpClient.request(url,'post',req, cache, (data)=>{
        if(callback){callback(data)}
    });
};

const producerRankList = (payload,callback,cache=true) => {
    //超级节点排名
    let req = {
        first:payload.first || 0,
        pageSize:payload.pageSize || 10,
        name:payload.name || '',
    };
    let url = Constants.SERVERURL+'/productor/getListOrderByRank';
    HttpClient.request(url,'post',req, cache, (data)=>{
        if(callback){callback(data)}
    });
};

const producerVotedList = (payload,callback,cache=false) => {
    //超级节点已投
    let req = {
        first:payload.first || 0,
        pageSize:payload.pageSize || 10,
        account:payload.account || '',
    };
    let url = Constants.SERVERURL+'/productor/getAlreadyVoteList';
    HttpClient.request(url,'post',req, cache, (data)=>{
        if(callback){callback(data)}
    });
};

const agentRecommendList = (payload,callback,cache=true) => {
    //投票代理
    let req = {
        first:payload.first || 0,
        pageSize:payload.pageSize || 10,
    };
    let url = Constants.SERVERURL+'/productor/getSuperAgentList';
    HttpClient.request(url,'post',req, cache, (data)=>{
        if(callback){callback(data)}
    });
};

//-----token-----

const accountTokenList = ()=>{

};

const accountTokenAdd = (token)=>{

};

const accountTokenDel = (token)=>{

};

const tokenDetail = (token)=>{

};

const tokenList = ()=>{

};

const tokenSearch = ()=>{

};

const tokenTradeList = (token)=>{

};

const tokenTradeDetail = (trade)=>{

};

//-----market-----

/**
 * 获取代币信息列表
 *
 * request {fist:'分页初始点',pageSize:'每页展示最大条数',order:'排序字段 0系统维护默认排序 1按最新价排序 2按跌涨幅排序',uporDow:'1 升序排序   0降序排序'}
 * response {marketId:行情id,"baseVolume":交易量,"lastDollar":最新成交价  美元,"lastRMB":最新成交价 人民币,"marketcap":总市值(美元),"pair":交易对,"percentChange":涨跌百分比,"tokenId":代币id,"name":代币名称,
 *                "site":代币网址,"img":代币图片,"contractAccount":合约账号}
 * @return
 */
const tokenMarketList = (callback,cache=false)=>{
    let req = {};
    let url = Constants.SERVERURL+'/token/getPageList';
    HttpClient.request(url,'post',req, cache, (data)=>{
        if(callback){callback(data)}
    });
};

/**
 * 获取代币行情详细
 * request {id:行情id marketId}
 * response {marketId:行情id,"openDollar":开盘价 美元,"openRMB":开盘价 人民币,"high24HR":24小时最高价,"baseVolume":交易量,"lastDollar":最新成交价  美元,"lastRMB":最新成交价 人民币,
 *                 "marketcap":总市值(美元),"pair":交易对,"percentChange":涨跌百分比,"tokenId":代币id,"name":代币名称,"supply":供应量 发行量,"low24HR":24最低价}
 * @return
 */
const tokenMarketDetail = (marketId,callback,cache=false)=>{
    let req = {
        id:marketId
    };
    let url = Constants.SERVERURL+'/token/getMarket';
    HttpClient.request(url,'post',req, cache, (data)=>{
        if(callback){callback(data)}
    });
};


//-----dapp-----

const dappList = (callback,cache=false)=>{
    let req = {};
    let url = Constants.SERVERURL+'/dapp/list';
    HttpClient.request(url,'post',req, cache, (data)=>{
        if(callback){callback(data)}
    });
};

const dappDetail = (callback,cache=false)=>{

};

const dappBanner = (callback,cache=false)=>{
    let req = {};
    let url = Constants.SERVERURL+'/banner/list';
    HttpClient.request(url,'post',req, cache, (data)=>{
        if(callback){callback(data)}
    });
};

//-----address-----
let AddressListKey = 'AddressList1';

const addressList = (callback)=>{
    store.get(AddressListKey,callback);
};

const addressAdd = (payload,callback)=>{
    store.saveItem(AddressListKey,payload, callback);
};

const addressDel = (itemKey,itemValue,callback)=>{
    store.delItem(AddressListKey,itemKey,itemValue,callback);
};

const addressUpdate = (itemKey, itemValue, payload,callback)=>{
    store.updateItem(AddressListKey,itemKey, itemValue, payload,callback);
};

const addressDetail = (itemKey, itemValue,callback)=>{
    store.getItem(AddressListKey,itemKey,itemValue,callback);
};

//-----setting-----

const getLanguage = (callback)=>{
    store.get('language',(l)=>{
        if(callback) callback(l);
    });
};

const updateLanguage = (callback)=>{
    store.save('language',(l)=>{
        if(callback) callback(l);
    });
};

const nodeList = ()=>{

};

const nodeDefault = (node)=>{

};

const feedback = (content)=>{

};

export default {
    test,
    accountCreate,
    producerRecommendList,
    producerRankList,
    producerVotedList,
    agentRecommendList,
    accountTokenList,
    accountTokenAdd,
    accountTokenDel,
    tokenDetail,
    tokenList,
    tokenSearch,
    tokenTradeList,
    tokenTradeDetail,
    tokenMarketList,
    tokenMarketDetail,
    dappList,
    dappDetail,
    dappBanner,
    addressList,
    addressAdd,
    addressDel,
    addressUpdate,
    addressDetail,
    getLanguage,
    updateLanguage,
    nodeList,
    nodeDefault,
    feedback
}